package com.situ.mall.controller.back;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.pojo.bo.ProductSearchBO;
import com.situ.mall.pojo.entity.Category;
import com.situ.mall.pojo.entity.Product;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.JSONResult;

@Controller
@RequestMapping("/product")
public class ProductController {
	@Autowired
	private IProductService productService;
	@Autowired
	private ICategoryService categoryService;

	// /product/selectByPage.action?page=1&limit=10&name=&beginCreateTime=2017-03-15&endCreateTime=2019-10-01
	@ResponseBody
	@RequestMapping("/selectByPage")
	public JSONResult selectByPage(ProductSearchBO productSearchBO, Integer page, Integer limit) {
		JSONResult jsonResult = productService.selectByPage(productSearchBO, page, limit);
		return jsonResult;
	}
	
	@RequestMapping("/getProductListPage")
	public String getProductListPage() {
		return "/back/product_list";
	}
	
	@RequestMapping("/getProductInsertPage")
	public String getProductInsertPage(Model model) {
		List<Category> topCategoryList = categoryService.selectTopCategoryList();
		model.addAttribute("topCategoryList", topCategoryList);
		
		return "/back/product_insert";
	}
	
	@ResponseBody
	@RequestMapping("/insert")
	public JSONResult insert(Product product) {
		boolean isSuccess = productService.insert(product);
		
		return isSuccess ? JSONResult.ok("添加成功") : JSONResult.error("添加失败");
//		if (isSuccess) {
//			return JSONResult.ok("添加成功");
//		} else {
//			return JSONResult.error("添加失败");
//		}
	}
	
	@ResponseBody
	@RequestMapping("/selectSecondCategoryList")
	public JSONResult selectSecondCategoryList(Integer topCategoryId) {
		List<Category> list = categoryService.selectSecondCategoryList(topCategoryId);
		return JSONResult.ok(list);
	}
}
